<?php
require __DIR__ . '/autoload.php';
require __DIR__ . '/../helper.php';

$fp = fopen("./data/esData.log", "r");
$i=0;
$body = "";
$host = "http://127.0.0.1:9200";
$es_template = $host . "/_template";
$es_bulk = $host . "/_bulk";
// 创建es模板
$template = file_get_contents("./data/esTemplate.json");
fly_curl($es_template."/es", $template);
// 重置日志文件
file_put_contents("./logs/esbulk.log", "");
while ($line = fgets($fp)) {
    $i++;
    $body .= $line;
    if ($i>=1000) {
        $update_res = fly_curl($es_bulk, $body ."\n");
        $success = substr_count($update_res, '"successful"');
        echo $msg =  $es_bulk."-" . "成功写入".$success."条数据"."\n";
        file_put_contents("./logs/esbulk.log", $msg, FILE_APPEND );
        $i=0; $body = "";
    }
}
if ($body) {
    $update_res = fly_curl($es_bulk, $body ."\n");
    $success = substr_count($update_res, '"successful"');
    echo $msg =  $es_bulk."-" . "成功写入".$success."条数据"."\n";
    file_put_contents("./logs/esbulk.log", $msg, FILE_APPEND );
}
echo "Done......";


